{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyspark"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sc = pyspark.SparkContext()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 利用ArcGIS的arcpy包进行序列化，要求是你的Python环境使用的是ArcGIS的Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import arcpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "feat = arcpy.FeatureSet(\"./data/dltb.shp\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 利用arcpy的da包，将数据读取成一个元祖，由几何对象和地类名称两个字段组成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "featlist = [(row[0],row[1]) \\\n",
    "            for row in arcpy.da.SearchCursor(feat,[\"SHAPE@\",\"DLMC\"])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(<Polygon object at 0x269e97b4588[0x269e97a99b8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97b45c0[0x269e97a9a08]>, '园地'),\n",
       " (<Polygon object at 0x269e97b45f8[0x269e97a9a58]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4630[0x269e97a9aa8]>, '园地'),\n",
       " (<Polygon object at 0x269e97b4668[0x269e97a9b20]>, '园地'),\n",
       " (<Polygon object at 0x269e97b46a0[0x269e97a9be8]>, '园地'),\n",
       " (<Polygon object at 0x269e97b46d8[0x269e97a9c38]>, '园地'),\n",
       " (<Polygon object at 0x269e97b4710[0x269e97a9c88]>, '园地'),\n",
       " (<Polygon object at 0x269e97b4748[0x269e97a9cd8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97b4780[0x269e97a9d50]>, '林地'),\n",
       " (<Polygon object at 0x269e97b47b8[0x269e97a9da0]>, '园地'),\n",
       " (<Polygon object at 0x269e97b47f0[0x269e97a9e18]>, '园地'),\n",
       " (<Polygon object at 0x269e97b4828[0x269e97a9eb8]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4860[0x269e97a9f08]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4898[0x269e97a9f80]>, '林地'),\n",
       " (<Polygon object at 0x269e97b48d0[0x269e97a9fa8]>, '园地'),\n",
       " (<Polygon object at 0x269e97b4908[0x269e97a9ad0]>, '园地'),\n",
       " (<Polygon object at 0x269e97b4940[0x269e97a9dc8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97b4978[0x269e97a9e68]>, '林地'),\n",
       " (<Polygon object at 0x269e97b49b0[0x269e97a9e90]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4a20[0x269e525afd0]>, '旱地'),\n",
       " (<Polygon object at 0x269e97b49e8[0x269e97ce030]>, '园地'),\n",
       " (<Polygon object at 0x269e97b4a58[0x269e97ce080]>, '旱地'),\n",
       " (<Polygon object at 0x269e97b4a90[0x269e97ce0d0]>, '旱地'),\n",
       " (<Polygon object at 0x269e97b4ac8[0x269e97ce120]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4b00[0x269e97ce170]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4b38[0x269e97ce1c0]>, '旱地'),\n",
       " (<Polygon object at 0x269e97b4be0[0x269e97a9990]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4c18[0x269e97ce238]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4c50[0x269e97ce288]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4c88[0x269e97ce2d8]>, '园地'),\n",
       " (<Polygon object at 0x269e97b4cc0[0x269e97ce328]>, '园地'),\n",
       " (<Polygon object at 0x269e97b4cf8[0x269e97ce378]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4d30[0x269e97ce3c8]>, '园地'),\n",
       " (<Polygon object at 0x269e97b4d68[0x269e97ce418]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4da0[0x269e97ce468]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4dd8[0x269e97ce4b8]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4e10[0x269e97ce508]>, '园地'),\n",
       " (<Polygon object at 0x269e97b4e48[0x269e97ce558]>, '园地'),\n",
       " (<Polygon object at 0x269e97b4e80[0x269e97ce5a8]>, '园地'),\n",
       " (<Polygon object at 0x269e97b4eb8[0x269e97ce5f8]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4ef0[0x269e97ce648]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4f28[0x269e97ce698]>, '林地'),\n",
       " (<Polygon object at 0x269e97b4f60[0x269e97ce6e8]>, '园地'),\n",
       " (<Polygon object at 0x269e97b4f98[0x269e97ce738]>, '旱地'),\n",
       " (<Polygon object at 0x269e97b4fd0[0x269e97ce788]>, '林地'),\n",
       " (<Polygon object at 0x269e97c42e8[0x269e97ce7d8]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4048[0x269e97ce828]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4080[0x269e97ce878]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c40b8[0x269e97ce8c8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c40f0[0x269e97ce918]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4128[0x269e97ce968]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4160[0x269e97ce9b8]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4198[0x269e97cea08]>, '林地'),\n",
       " (<Polygon object at 0x269e97c41d0[0x269e97cea58]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4208[0x269e97ceaa8]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4240[0x269e97ceaf8]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4278[0x269e97ceb48]>, '林地'),\n",
       " (<Polygon object at 0x269e97c42b0[0x269e97ceb98]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4438[0x269e97cebe8]>, '林地'),\n",
       " (<Polygon object at 0x269e97c43c8[0x269e97cec38]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4400[0x269e97cec88]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4358[0x269e97cecd8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4390[0x269e97ced28]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4470[0x269e97ced78]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c44a8[0x269e97cedc8]>, '林地'),\n",
       " (<Polygon object at 0x269e97c44e0[0x269e97cee18]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4518[0x269e97cee68]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4550[0x269e97ceeb8]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4588[0x269e97cef08]>, '园地'),\n",
       " (<Polygon object at 0x269e97c45c0[0x269e97cef58]>, '林地'),\n",
       " (<Polygon object at 0x269e97c45f8[0x269e97cefa8]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4630[0x269e97d0030]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4668[0x269e97d0080]>, '园地'),\n",
       " (<Polygon object at 0x269e97c46a0[0x269e97d00d0]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c46d8[0x269e97d0120]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4710[0x269e97d0170]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4748[0x269e97d01c0]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4780[0x269e97d0210]>, '园地'),\n",
       " (<Polygon object at 0x269e97c47b8[0x269e97d0260]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c47f0[0x269e97d02b0]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4828[0x269e97d0300]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4860[0x269e97d0350]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4898[0x269e97d03a0]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c48d0[0x269e97d03f0]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4908[0x269e97d0440]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4940[0x269e97d0490]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4978[0x269e97d04e0]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c49b0[0x269e97d0530]>, '林地'),\n",
       " (<Polygon object at 0x269e97c49e8[0x269e97d0580]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4a20[0x269e97d05d0]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4a58[0x269e97d0620]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4a90[0x269e97d0670]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4ac8[0x269e97d06c0]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4b00[0x269e97d0710]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4b38[0x269e97d0760]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4b70[0x269e97d07b0]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4ba8[0x269e97d0800]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4be0[0x269e97d0850]>, '农村居民点用地'),\n",
       " (<Polygon object at 0x269e97c4c18[0x269e97d08a0]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4c50[0x269e97d08f0]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4c88[0x269e97d0940]>, '农村居民点用地'),\n",
       " (<Polygon object at 0x269e97c4cc0[0x269e97d0990]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4cf8[0x269e97d09e0]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4d30[0x269e97d0a30]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4d68[0x269e97d0a80]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4da0[0x269e97d0ad0]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4dd8[0x269e97d0b20]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4e10[0x269e97d0b70]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4e48[0x269e97d0bc0]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4e80[0x269e97d0c10]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4eb8[0x269e97d0c60]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4ef0[0x269e97d0cb0]>, '农村居民点用地'),\n",
       " (<Polygon object at 0x269e97c4f28[0x269e97d0d00]>, '旱地'),\n",
       " (<Polygon object at 0x269e97c4f60[0x269e97d0d50]>, '园地'),\n",
       " (<Polygon object at 0x269e97c4f98[0x269e97d0da0]>, '林地'),\n",
       " (<Polygon object at 0x269e97c4fd0[0x269e97d0df0]>, '农村居民点用地'),\n",
       " (<Polygon object at 0x269e97d4048[0x269e97d0e40]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4080[0x269e97d0e90]>, '园地'),\n",
       " (<Polygon object at 0x269e97d40b8[0x269e97d0ee0]>, '园地'),\n",
       " (<Polygon object at 0x269e97d40f0[0x269e97d0f30]>, '农村居民点用地'),\n",
       " (<Polygon object at 0x269e97d4128[0x269e97d0f80]>, '园地'),\n",
       " (<Polygon object at 0x269e97d4160[0x269e97d0fd0]>, '园地'),\n",
       " (<Polygon object at 0x269e97d4198[0x269e97d5058]>, '园地'),\n",
       " (<Polygon object at 0x269e97d41d0[0x269e97d50a8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4208[0x269e97d50f8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4240[0x269e97d5148]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4278[0x269e97d5198]>, '林地'),\n",
       " (<Polygon object at 0x269e97d42b0[0x269e97d51e8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d42e8[0x269e97d5238]>, '农村居民点用地'),\n",
       " (<Polygon object at 0x269e97d4320[0x269e97d5288]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4358[0x269e97d52d8]>, '园地'),\n",
       " (<Polygon object at 0x269e97d4390[0x269e97d5328]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d43c8[0x269e97d5378]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4400[0x269e97d53c8]>, '园地'),\n",
       " (<Polygon object at 0x269e97d4438[0x269e97d5418]>, '林地'),\n",
       " (<Polygon object at 0x269e97d4470[0x269e97d5468]>, '园地'),\n",
       " (<Polygon object at 0x269e97d44a8[0x269e97d54b8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d44e0[0x269e97d5508]>, '园地'),\n",
       " (<Polygon object at 0x269e97d4518[0x269e97d5558]>, '林地'),\n",
       " (<Polygon object at 0x269e97d4550[0x269e97d55a8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4588[0x269e97d55f8]>, '林地'),\n",
       " (<Polygon object at 0x269e97d45c0[0x269e97d5648]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d45f8[0x269e97d5698]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4630[0x269e97d56e8]>, '园地'),\n",
       " (<Polygon object at 0x269e97d4668[0x269e97d5738]>, '园地'),\n",
       " (<Polygon object at 0x269e97d46a0[0x269e97d5788]>, '林地'),\n",
       " (<Polygon object at 0x269e97d46d8[0x269e97d57d8]>, '园地'),\n",
       " (<Polygon object at 0x269e97d4710[0x269e97d5828]>, '园地'),\n",
       " (<Polygon object at 0x269e97d4748[0x269e97d5878]>, '林地'),\n",
       " (<Polygon object at 0x269e97d4780[0x269e97d58c8]>, '林地'),\n",
       " (<Polygon object at 0x269e97d47b8[0x269e97d5918]>, '园地'),\n",
       " (<Polygon object at 0x269e97d47f0[0x269e97d5968]>, '林地'),\n",
       " (<Polygon object at 0x269e97d4828[0x269e97d59b8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4860[0x269e97d5a08]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4898[0x269e97d5a58]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d48d0[0x269e97d5aa8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4908[0x269e97d5af8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4940[0x269e97d5b48]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4978[0x269e97d5b98]>, '林地'),\n",
       " (<Polygon object at 0x269e97d49b0[0x269e97d5be8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d49e8[0x269e97d5c38]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4a20[0x269e97d5c88]>, '林地'),\n",
       " (<Polygon object at 0x269e97d4a58[0x269e97d5cd8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4a90[0x269e97d5d28]>, '林地'),\n",
       " (<Polygon object at 0x269e97d4ac8[0x269e97d5d78]>, '林地'),\n",
       " (<Polygon object at 0x269e97d4b00[0x269e97d5dc8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4b38[0x269e97d5e18]>, '园地'),\n",
       " (<Polygon object at 0x269e97d4b70[0x269e97d5e68]>, '林地'),\n",
       " (<Polygon object at 0x269e97d4ba8[0x269e97d5eb8]>, '园地'),\n",
       " (<Polygon object at 0x269e97d4be0[0x269e97d5f08]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4c18[0x269e97d5f58]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4c50[0x269e97d5fa8]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4c88[0x269e97d8030]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4cc0[0x269e97d8080]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4cf8[0x269e97d80d0]>, '园地'),\n",
       " (<Polygon object at 0x269e97d4d30[0x269e97d8120]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4d68[0x269e97d8170]>, '林地'),\n",
       " (<Polygon object at 0x269e97d4da0[0x269e97d81c0]>, '林地'),\n",
       " (<Polygon object at 0x269e97d4dd8[0x269e97d8210]>, '林地'),\n",
       " (<Polygon object at 0x269e97d4e10[0x269e97d8260]>, '林地'),\n",
       " (<Polygon object at 0x269e97d4e48[0x269e97d82b0]>, '其他独立建设用地'),\n",
       " (<Polygon object at 0x269e97d4e80[0x269e97d8300]>, '旱地'),\n",
       " (<Polygon object at 0x269e97d4eb8[0x269e97d8350]>, '其他独立建设用地'),\n",
       " (<Polygon object at 0x269e97d4ef0[0x269e97d83a0]>, '园地'),\n",
       " (<Polygon object at 0x269e97d4f28[0x269e97d83f0]>, '园地'),\n",
       " (<Polygon object at 0x269e97d4f60[0x269e97d8440]>, '林地'),\n",
       " (<Polygon object at 0x269e97d4f98[0x269e97d8490]>, '其他独立建设用地'),\n",
       " (<Polygon object at 0x269e97d4fd0[0x269e97d84e0]>, '其他独立建设用地'),\n",
       " (<Polygon object at 0x269e97da048[0x269e97d8530]>, '林地'),\n",
       " (<Polygon object at 0x269e97da080[0x269e97d8580]>, '林地'),\n",
       " (<Polygon object at 0x269e97da0b8[0x269e97d85d0]>, '林地')]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "featlist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将对象直接转换为PySpark的RDD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "frdd = sc.parallelize(featlist)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "192"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frdd.count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 按照地类名称进行面积统计"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 其中，f[0]是一个polygon对象，area是用来计算对象的面积的属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('林地', 4250805.430885682),\n",
       " ('旱地', 1062114.5405802839),\n",
       " ('园地', 974189.4862168157),\n",
       " ('其他独立建设用地', 257737.88656116943),\n",
       " ('农村居民点用地', 39557.913537067856)]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frdd.map(lambda f : (f[1],f[0].area)).reduceByKey(lambda x,y:(x+y)).collect()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
